﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2010 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器 V4.3.3.6 自动创建于 2010/4/21 17:07:35
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace OPMS_IBS_2.SqlServerDAL
{
	/// <summary>
	/// 数据访问类 Token
	/// </summary>
	public class Token : DALHelper
	{
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OPMS_IBS_2.Model.Token model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("INSERT INTO Token(");
            strSql.Append("Name,OwnTimeTable)");
			strSql.Append(" VALUES (");
            strSql.Append("@in_Name,@in_OwnTimeTable)");
			DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_Name", DbType.AnsiString, model.Name),
				OPMS_IBS_2Helper.CreateInDbParameter("@in_OwnTimeTable", DbType.Guid, model.OwnTimeTable)};

			return OPMS_IBS_2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public int Update(OPMS_IBS_2.Model.Token model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("UPDATE Token SET ");
			strSql.Append("Name=@in_Name,");
			strSql.Append("OwnTimeTable=@in_OwnTimeTable");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_Name", DbType.AnsiString, model.Name),
				OPMS_IBS_2Helper.CreateInDbParameter("@in_OwnTimeTable", DbType.Guid, model.OwnTimeTable),
				OPMS_IBS_2Helper.CreateInDbParameter("@in_ID", DbType.Guid, model.ID)};
			return OPMS_IBS_2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public int Delete(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM Token ");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			return OPMS_IBS_2Helper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT COUNT(1) FROM Token");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			object obj = OPMS_IBS_2Helper.ExecuteScalar(CommandType.Text, strSql.ToString(), cmdParms);
			return OPMS_IBS_2Helper.GetInt(obj) > 0;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OPMS_IBS_2.Model.Token GetModel(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT * FROM Token ");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			OPMS_IBS_2.Model.Token model = null;
			using (DbDataReader dr = OPMS_IBS_2Helper.ExecuteReader(CommandType.Text, strSql.ToString(), cmdParms))
			{
				while (dr.Read())
				{
					model = GetModel(dr);
				}
				return model;
			}
		}

		/// <summary>
		/// 获取泛型数据列表
		/// </summary>
		public List<OPMS_IBS_2.Model.Token> GetList()
		{
			StringBuilder strSql = new StringBuilder("SELECT * FROM Token");
			using (DbDataReader dr = OPMS_IBS_2Helper.ExecuteReader(CommandType.Text, strSql.ToString(), null))
			{
				List<OPMS_IBS_2.Model.Token> lst = GetList(dr);
				return lst;
			}
		}

		/// <summary>
		/// 得到数据条数
		/// </summary>
		public int GetCount(string condition)
		{
			return OPMS_IBS_2Helper.GetCount("Token", condition);
		}

		/// <summary>
		/// 分页获取泛型数据列表
		/// </summary>
		public List<OPMS_IBS_2.Model.Token> GetPageList(int pageSize, int pageIndex, string fldSort, bool fldDir, string condition)
		{
			using (DbDataReader dr = OPMS_IBS_2Helper.GetPageList("Token", pageSize, pageIndex, fldSort, fldDir, condition))
			{
				List<OPMS_IBS_2.Model.Token> lst = GetList(dr);
				return lst;
			}
		}

		#region -------- 私有方法，通常情况下无需修改 --------

		/// <summary>
		/// 由一行数据得到一个实体
		/// </summary>
		private OPMS_IBS_2.Model.Token GetModel(DbDataReader dr)
		{
			OPMS_IBS_2.Model.Token model = new OPMS_IBS_2.Model.Token();
			model.ID = OPMS_IBS_2Helper.GetGuid(dr["ID"]);
			model.Name = OPMS_IBS_2Helper.GetString(dr["Name"]);
			model.OwnTimeTable = OPMS_IBS_2Helper.GetGuid(dr["OwnTimeTable"]);
			return model;
		}

		/// <summary>
		/// 由DbDataReader得到泛型数据列表
		/// </summary>
		private List<OPMS_IBS_2.Model.Token> GetList(DbDataReader dr)
		{
			List<OPMS_IBS_2.Model.Token> lst = new List<OPMS_IBS_2.Model.Token>();
			while (dr.Read())
			{
				lst.Add(GetModel(dr));
			}
			return lst;
		}

		#endregion

        #region User Code

        public Guid GetIdByName(string tokenName)
	    {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID FROM Token");
            strSql.Append(" WHERE Name=@in_name");
            DbParameter[] cmdParms = {
				OPMS_IBS_2Helper.CreateInDbParameter("@in_name", DbType.String, tokenName)};

            object obj = OPMS_IBS_2Helper.ExecuteScalar(CommandType.Text, strSql.ToString(), cmdParms);
            return OPMS_IBS_2Helper.GetGuid(obj);
	    }

        #endregion
    }
}
